package com.qcxy.gzy.view.dialog;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.qxcy.gzy.util.RelayoutTool;


public class BaseDialog extends Dialog {

    private static final float UI_STANDARD_width = 1280;

    BaseDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = View.inflate(getContext(), layoutResID, null);
        this.setContentView(view);
    }

    @Override
    public void setContentView(@NonNull View view) {
        RelayoutTool.relayoutViewHierarchy(view, getScale());
        super.setContentView(view);
    }

    private float getScale() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        float width = displayMetrics.widthPixels;
        return width / UI_STANDARD_width;
    }

    @Override
    public void show() {
        super.show();
        Window dialogWindow = getWindow();
        if (dialogWindow == null) return;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.85); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }
}